package com.solution.admin.customer.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CustomerCustomFieldEntity;
import com.solution.admin.customer.service.CustomerCustomFieldService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户属性自定义web控制层
 * @author solin
 * @date 2017-09-27 09:19:24
 */
@RequestMapping("/customerCustomField")
@Controller
public class CustomerCustomFieldController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CustomerCustomFieldController.class);

	@Autowired
	private CustomerCustomFieldService customerCustomFieldService;
	
	/**
	 * 跳转客户属性自定义查询页面
	 * @param CustomerCustomField 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-27 09:19:24
	 */
	@RequestMapping("/toListCustomerCustomFieldPage")
	public String toListCustomerCustomFieldPage(CustomerCustomFieldEntity customerCustomField, HttpServletRequest request) {
		return "customer/customerCustomField_list";
	}

	/**
	 * 分页查询客户属性自定义列表
	 * @param customerCustomField 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-27 09:19:24
	 */
	@RequestMapping("/listCustomerCustomFieldPage")
	@ResponseBody
	public JSONGrid listCustomerCustomFieldPage(CustomerCustomFieldEntity customerCustomField, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("fieldName", customerCustomField.getFieldName());
		try {
			PageBean pageBean = customerCustomFieldService.listCustomerCustomFieldPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户属性自定义失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 查询客户属性自定义列表
	 * @return
	 * @author: solin
	 * @date: 2017年9月28日 上午9:13:49
	 */
	@RequestMapping("/listCustomerCustomField")
	@ResponseBody
	public List<CustomerCustomFieldEntity> listCustomerCustomField(CustomerCustomFieldEntity customerCustomField, HttpServletRequest request) {
		List<CustomerCustomFieldEntity> list = new ArrayList<CustomerCustomFieldEntity>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
		    paramMap.put("status", 1);
			list = customerCustomFieldService.listCustomerCustomField(paramMap);
		} catch (Exception e) {
			handleException("获取客户属性自定义列表数据失败", logger, e);
		}
		return list;
	}
	
	/**
	 * 查询客户属性自定义信息
	 * @param id
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年9月28日 上午9:28:04
	 */
	@RequestMapping("/getCustomerCustomField")
	@ResponseBody
	public CustomerCustomFieldEntity getCustomerCustomField(Long id, HttpServletRequest request) {
		CustomerCustomFieldEntity customFiled = new CustomerCustomFieldEntity();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			customFiled = customerCustomFieldService.getCustomerCustomFieldById(id);
		} catch (Exception e) {
			handleException("获取客户属性自定义数据失败", logger, e);
		}
		return customFiled;
	}
	
	/**
	 * 跳转到选择属性界面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年9月28日 上午8:57:20
	 */
	@RequestMapping("/toSelectCustomerCustomField")
	public String toSelectCustomerCustomField(HttpServletRequest request, Model model) {
		model.addAttribute("fieldId", request.getParameter("fieldId"));
		model.addAttribute("fieldType", request.getParameter("fieldType"));
		try {
			String fieldName = request.getParameter("fieldName");
			if (StringUtil.isNotEmpty(fieldName)) {
				fieldName = new String(fieldName.getBytes("ISO-8859-1"),"UTF-8");
			}
			model.addAttribute("fieldName", fieldName);
			String fieldValue = request.getParameter("fieldValue");
			if (StringUtil.isNotEmpty(fieldValue)) {
				fieldValue = new String(fieldValue.getBytes("ISO-8859-1"),"UTF-8");
			}
			model.addAttribute("fieldValue", fieldValue);
		} catch (UnsupportedEncodingException e) {
		}
		return "customer/customerCustomField_select";
	}
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-27 09:19:24
	 */
	@RequestMapping("/toAddCustomerCustomField")
	public String toAddCustomerCustomField(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/customerCustomField_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-27 09:19:24
	 */
	@RequestMapping("/toUpdateCustomerCustomField")
	public String toUpdateCustomerCustomField(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerCustomFieldEntity customerCustomField = customerCustomFieldService.getCustomerCustomFieldById(id);
			request.setAttribute("customerCustomField", customerCustomField);
		} catch (Exception e) {
			handleException("跳转编辑客户属性自定义页面错误", logger, e);
		}
		return "customer/customerCustomField_form";
	}


	/**
	 * 新增或保存客户属性自定义
	 * @param request 请求对象
	 * @param customerCustomFieldEntity 客户属性自定义实体
	 * @return
	 * @author solin
 	 * @date 2017-09-27 09:19:24
	 */
	@RequestMapping("/saveOrUpdateCustomerCustomField")
	@ResponseBody
	public Map<String, Object> saveCustomerCustomField(HttpServletRequest request, CustomerCustomFieldEntity customerCustomFieldEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				customerCustomFieldEntity.setModifier(this.getUserCode());
				result = customerCustomFieldService.updateCustomerCustomField(customerCustomFieldEntity);
			}else{
				customerCustomFieldEntity.setCreater(this.getUserCode());
				customerCustomFieldEntity.setIsDelete(0);
				result = (int)customerCustomFieldService.saveCustomerCustomField(customerCustomFieldEntity);
				if (result > 1) {
					result = 1;
				}
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加客户属性自定义失败", logger, e);
		}
		return resultMap;
	}
	
	/**
	 * 删除客户自定义字段
	 * @param request
	 * @param customerCustomFieldEntity
	 * @return
	 * @author: solin
	 * @date: 2017年9月27日 下午7:56:14
	 */
	@RequestMapping("/deleteCustomerCustomField")
	@ResponseBody
	public Map<String, Object> deleteCustomerCustomField(HttpServletRequest request, CustomerCustomFieldEntity customerCustomFieldEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			result = customerCustomFieldService.deleteCustomerCustomFieldById(customerCustomFieldEntity.getId());
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("删除客户属性自定义失败", logger, e);
		}
		return resultMap;
	}
	
}
